package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    CheckItemService checkItemService;

    private CheckItem checkItem;

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result  add(@RequestBody CheckItem checkItem){

        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }

        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult=  checkItemService.findPage(
                queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return pageResult;

    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE123')")
    public Result delete(Integer id ){
        try {
            checkItemService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }


    @RequestMapping("/findById")
    public CheckItem findById(Integer id){
            CheckItem checkItem = checkItemService.findById(id);
            return checkItem;

    }


    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public  Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public List<CheckItem> findAll(){
       List<CheckItem> list= checkItemService.findAll();
       return list;
    }

    @RequestMapping("/xiazai")
    public void xiazai() throws Exception {
        List<CheckItem> list= checkItemService.findAll();
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("checkitem");
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("id");
        row.createCell(1).setCellValue("code");
        row.createCell(2).setCellValue("name");
        row.createCell(3).setCellValue("sex");
        row.createCell(4).setCellValue("age");
        row.createCell(5).setCellValue("price");
        row.createCell(6).setCellValue("type");
        row.createCell(7).setCellValue("attention");
        row.createCell(8).setCellValue("remark");
        for(int i =1;i<=list.size();i++){
            XSSFRow rowi = sheet.createRow(i);
            rowi.createCell(0).setCellValue(list.get(i-1).getId());
            rowi.createCell(1).setCellValue(list.get(i-1).getCode());
            rowi.createCell(2).setCellValue(list.get(i-1).getName());
            rowi.createCell(3).setCellValue(list.get(i-1).getSex());
            rowi.createCell(4).setCellValue(list.get(i-1).getAge());
            rowi.createCell(5).setCellValue(list.get(i-1).getPrice());
            rowi.createCell(6).setCellValue(list.get(i-1).getType());
            rowi.createCell(7).setCellValue(list.get(i-1).getAttention());
            rowi.createCell(8).setCellValue(list.get(i-1).getRemark());
        }
        FileOutputStream outputStream = new FileOutputStream("D:\\haha.xlsx");
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        workbook.close();
    }

}
